package com.ifatter.tick.data;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@SuppressWarnings("serial")
public class DataServlet extends HttpServlet implements DataOp {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/plain");
		String op = (String) req.getParameter(OP);
		if (INSERT.equals(op)) {
			insert(req, resp);
		} else if (DELETE.equals(op)) {
			delete(req, resp);
		} else if (UPDATE.equals(op)) {
			update(req, resp);
		} else if (QUERY.equals(op)) {
			query(req, resp);
		} else {
			resp.getWriter().println(ERROR_INFO);
		}

	}

	private void insert(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		DatastoreService datastore = DatastoreServiceFactory
				.getDatastoreService();
		String entityKey = (String) req.getParameter(ENTITY);
		String id = (String) req.getParameter("id");
		Key k = KeyFactory.createKey(entityKey, id);
		System.out.println(k);
		Entity entity = new Entity(entityKey, k);
		@SuppressWarnings("unchecked")
		Enumeration<String> en = req.getParameterNames();
		while (en.hasMoreElements()) {
			String key = en.nextElement();
			String value = (String) req.getParameter(key);
			entity.setProperty(key, value);
			System.out.println(key + ":" + value + "\n");
		}
		datastore.put(entity);
		resp.getWriter().println("success");

	}

	private void delete(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {

	}

	private void update(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {

	}

	private void query(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		DatastoreService datastore = DatastoreServiceFactory
				.getDatastoreService();
		StringBuffer sb = new StringBuffer();
		String entityKey = (String) req.getParameter(ENTITY);
		String id = (String) req.getParameter("id");
		Key k = KeyFactory.createKey(entityKey, id);
		System.out.println(k);
		try {
			Entity entity = datastore.get(k);
			Map<String, Object> map = entity.getProperties();
			Set<Entry<String, Object>> set = map.entrySet();
			for (Entry<String, Object> e : set) {
				sb.append(e.getKey()).append(":").append(e.getValue())
						.append("/n");
			}
			resp.getWriter().println(sb.toString());
		} catch (EntityNotFoundException e) {
			e.printStackTrace();
		}
	}

}
